/**
 * 
 */
package neptune.web.client.view.components;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class UserTable extends Composite {

	public interface UserTableListener {

		public void addUserPressed(UserTable sender);

		public void removeUserPressed(UserTable sender);

	}

	private FlowPanel userTable;

	private UserTableListener listener;
	private ClickListener removeListener = new ClickListener() {
		public void onClick(Widget arg0) {
			listener.removeUserPressed(UserTable.this);
		}
	};
	private ClickListener addListener = new ClickListener() {
		public void onClick(Widget arg0) {
			listener.addUserPressed(UserTable.this);
		}
	};

	public UserTable(String tableHeader, UserTableListener listener, int height) {
		this.listener = listener;

		VerticalPanel userPanel = new VerticalPanel();
		Label header = new Label(tableHeader);

		userPanel.add(header);

		ScrollPanel userTablePanel = new ScrollPanel();
		userTable = new FlowPanel();
		userTablePanel.setWidget(userTable);
		userTablePanel.setHeight(height + "px");

		userPanel.add(userTablePanel);

		HorizontalPanel buttonPanel = new HorizontalPanel();
		Button add = new Button("Add User");
		add.addClickListener(addListener);
		Button remove = new Button("Remove User");
		remove.addClickListener(removeListener);

		buttonPanel.add(add);
		buttonPanel.add(remove);

		userPanel.add(buttonPanel);

		// Styles
		header.setStyleName("ExperimentView-userTableHeader");
		userPanel.setStyleName("ExperimentView-userTablePanel");
		userTable.setStyleName("ExperimentView-userTable");
		buttonPanel.setStyleName("ExperimentView-userButtonPanel");
		add.setStyleName("ExperimentView-userPanelButton");
		remove.setStyleName("ExperimentView-userPanelButton");

		initWidget(userPanel);
	}

	public void addUsername(String username) {
		Label label = new Label(username);
		label.setStyleName("ExperimentView-userTableEntry");

		userTable.add(label);
	}

	public String[] getUsernames() {
		String[] users = new String[userTable.getWidgetCount()];

		for (int i = 0; i < users.length; i++) {
			users[i] = ((Label) userTable.getWidget(i)).getText();
		}

		return users;
	}

	public void removeUsername(String username) {
		int rowNumber = userTable.getWidgetCount();
		for (int i = 0; i < rowNumber; i++) {
			if (((Label) userTable.getWidget(i)).getText().equals(username)) {
				userTable.remove(i);
				return;
			}
		}
	}

	public void removeAllUsernames() {
		userTable.clear();
	}

	public UserTableListener getListener() {
		return listener;
	}

	public void setListener(UserTableListener listener) {
		this.listener = listener;
	}
}
